package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.Util.WebUtil;
import com.erp.comm.ResultCode;
import com.erp.pojo.LoginInfo;
import com.erp.pojo.User;
import com.erp.service.LoginInfoService;
import com.erp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Date;

@RestController
@RequestMapping("login")
public class LoginController {
   @Autowired
  private LoginInfoService loginInfoService;
   @Autowired
   private UserService userService;

    @RequestMapping("login")
    public ResultCode login(String loginname, String pwd){
                //添加登录信息\
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setLoginname(loginname);
//获取请求ip地址
        loginInfo.setLoginip(WebUtil.getRequest().getRemoteAddr());
        loginInfo.setLogintime(new Date());
        loginInfoService.save(loginInfo);
        //设置查询条件
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("loginname",loginname).eq("pwd",pwd);
        User user = userService.getOne(qw);
        if (user !=null) {
            WebUtil.getSession().setAttribute("user",user);
            //HttpSession session = request.getSession();

            //loginname 和 pwd 进行 查询
            //给结果
            //调用service查询 有 登陆成功 无 登录失败n0
            //return rc;
            return ResultCode.SUCCESS_LOGIN;//登录成功
        }else
        {return ResultCode.FAIL_LOGIN;
        }//失败







    }
}
